﻿using AutoMapper;
using B.S.Procurement.Domain.SalesReturns;
using B.S.Procurement.Error;
using B.S.Procurement.Read.Api.Application.Command.ItemizedReturnsCommand;
using B.S.Procurement.Read.Api.DTO;
using MediatR;
using SqlSugar;

namespace B.S.Procurement.Read.Api.Application.CommandHander.ItemizedReturnsCommandHander
{
    public class QueryItemizedReturnsCommandHander : IRequestHandler<QueryItemizedReturnsCommand, ApiPaging<List<ItemizedReturnsDto>>>
    {
        private readonly IMapper mapper;
        private readonly ISqlSugarClient db;

        public QueryItemizedReturnsCommandHander(IMapper mapper, ISqlSugarClient db)
        {
            this.mapper = mapper;
            this.db = db;
        }

        public Task<ApiPaging<List<ItemizedReturnsDto>>> Handle(QueryItemizedReturnsCommand request, CancellationToken cancellationToken)
        {
            ApiPaging<List<ItemizedReturnsDto>> result = new ApiPaging<List<ItemizedReturnsDto>>();

            var item = db.Queryable<ItemizedReturns>()
                       .InnerJoin<SalesReturnsModel>((a, b) => a.SalesReturnsId == b.SalesReturnsId)
                       .Where((a, b) => a.Del == false)
                       .Select((a, b) => new ItemizedReturnsDto
                       {
                           ItemizedReturnsId = a.ItemizedReturnsId,
                           ProductsId = a.ProductsId,
                           OutboundStatus = a.OutboundStatus,
                           ProductNumber = a.ProductNumber,
                           Specification = a.Specification,
                           Unit = a.Unit,
                           Num = a.Num,
                           TaxPrice = a.TaxPrice,
                           IncludedPrice = a.IncludedPrice,
                           Money = a.Money,
                           SumMoney = a.SumMoney,
                           Rate = a.Rate,
                           Tax = a.Tax,
                           LotNumber = a.LotNumber,
                           Storage = a.Storage,
                           TicketType = a.TicketType,
                           ManufactureDate = a.ManufactureDate,
                           ExpirationDate = a.ExpirationDate,
                           ArrivalDate = a.ArrivalDate,
                           PurchaseDate = a.PurchaseDate,
                           Remark = a.Remark,
                           Del = a.Del,
                           SalesReturnsId = b.SalesReturnsId,
                           ClientId = b.ClientId,
                           SellThemes = b.SellThemes,
                           SalesNumber = b.SalesNumber,
                           ReturnType = b.ReturnType,
                           ReturnProgram = b.ReturnProgram,
                           ReturnTime = b.ReturnTime,
                           Salespeople = b.Salespeople,
                           AddPeople = b.AddPeople,
                           AddTime = b.AddTime,
                           ProjectId = b.ProjectId,
                       });

            result.TotalCount = item.Count();
            result.TotalPage = (int)Math.Ceiling(result.TotalCount * 1.0 / request.PageSize);
            item = item.OrderByDescending(a => a.ItemizedReturnsId).Skip((request.PageIndex - 1) * request.PageSize).Take(request.PageSize);

            result.Code = ApiEnum.success;
            result.Msg = "查询成功";
            result.Data = mapper.Map<List<ItemizedReturnsDto>>(item.ToList());

            return Task.FromResult(result);
        }
    }
}
